# -*- encoding: utf-8 -*-
"""
File Name: alarm_sender
Description:
Author : 'lirui'
date: 2024/2/8
Contact: marjey_lee@163.com
Change Activity:
2024/2/8:
"""
import ll
from modules.blk.alarm.blk_alert import BLKAlert
from projects.monitoring.common.do.alarm import Alarm


class AlarmSender:
    @staticmethod
    def _format_alarm(alarm: Alarm):
        """
        ["remind", "warning", "fatal"]
        """
        level = alarm.severity
        host_name = alarm.hostname
        ip = alarm.ip
        if host_name is None or len(host_name) == 0:
            host_name = ip
        alarm_name = f'{host_name}-{alarm.alarm_info}'
        if alarm.manager_ip is not None and alarm.manager_port is not None:
            alarm_content = f'{alarm_name}—Manager:{alarm.manager_ip}:{alarm.manager_port}'
        else:
            alarm_content = alarm_name
        return level, ip, alarm_name, alarm_content

    @staticmethod
    def _send(level, ip, alarm_name, alarm_content, action):
        """

        Args:
            level:
            ip:
            alarm_name:
            alarm_content:
            action:

        Returns:

        """
        if level == 'remind':
            return
        alarm_sender = BLKAlert()
        alarm_sender.send_internal_alarm(
            ip=ip,
            level=level,
            alarm_name=alarm_name,
            alarm_content=alarm_content,
            action=action,
        )

    @staticmethod
    def send_warning(alarms: list[Alarm]):
        for alarm in alarms:
            level, ip, alarm_name, alarm_content = AlarmSender._format_alarm(alarm)
            AlarmSender._send(
                level,
                ip,
                alarm_name,
                alarm_content,
                action='firing',
            )

    @staticmethod
    def send_recovery(alarms):
        for alarm in alarms:
            level, ip, alarm_name, alarm_content = AlarmSender._format_alarm(alarm)
            AlarmSender._send(
                level,
                ip,
                alarm_name,
                alarm_content,
                action='resolved',
            )
